import { assign } from 'lodash'
import config from '@/config'
import { fetchTableData, fetchZidanData, fetchRainData, fetchHeatMapData, fetchMeterUnitDetail } from '@/api/dailyview'

const defaultState = () => {
  return {
    defaultDate: config.nowDate, // 默认是当前时间前一天
    defaultPointRadius: 8,
    tablePageParams: {
      pageNum: 1,
      pageSize: 20
    },
    pageTotal: 0,
    tableLoading: false,
    tableData: null,
    currentSelectedMeterId: undefined,
    currentSelectedGasCls: 'CNG',
    currentSelectedMapDate: undefined,
    zidanLoading: false,
    rainList: [],
    rainListLoading: false,
    heatMapList: [],
    heatMapLoading: false,
    minimapCenter: [],
    heatMapSwiperState: false
  }
}

export default {
  namespaced: true,
  name: 'dailyview',
  state: defaultState(),
  getters: {},
  mutations: {
    REST_STATE(state) {
      state = assign(state, defaultState())
    },
    SET_TABLE_LOADING(state, flag) {
      state.tableLoading = flag
    },
    SET_TABLE_DATA(state, tableData) {
      state.tableData = tableData
    },
    SET_ZIDAN_LOADING(state, flag) {
      state.zidanLoading = flag
    },
    SET_RAIN_LOADING(state, flag) {
      state.rainListLoading = flag
    },
    SET_RAIN_LIST(state, list) {
      state.rainList = list
    },
    SET_CURRENT_SELECTED_GAS_CLS(state, type) {
      state.currentSelectedGasCls = type
    },
    SET_SELECTED_MAP_DATE(state, date) {
      state.currentSelectedMapDate = date
    },
    SET_HEAT_MAP_LIST(state, list) {
      state.heatMapList = list
    },
    SET_HEAT_MAP_LOADING(state, flag) {
      state.heatMapLoading = flag
    },
    SET_MINIMAP_CENTER(state, center) {
      state.minimapCenter = center
    },
    SET_CURRENT_METER_ID(state, id) {
      state.currentSelectedMeterId = id
    },
    SET_TABLE_PAGE_NUM(state, num) {
      state.tablePageParams.pageNum = num
    },
    SET_HEAT_MAP_SWIPER_STATE(state, flag) {
      state.heatMapSwiperState = flag
    }
  },
  actions: {
    clearStore(ctx) {
      ctx.commit('REST_STATE')
    },
    /**
     * desc: 获取表格数据
     * param {type}
     * return {type} void
     */
    async loadTableData(ctx, params) {
      ctx.commit('SET_TABLE_LOADING', true)
      const _tableData = await fetchTableData({
        meterGasClass: ctx.state.currentSelectedGasCls,
        tId: ctx.state.currentSelectedMapDate || ctx.state.defaultDate,
        ...ctx.state.tablePageParams,
        ...params
      })
      ctx.commit('SET_TABLE_LOADING', false)
      const { code, date = {} } = _tableData
      if (code === 200) {
        ctx.commit('SET_TABLE_DATA', date)
      }
    },
    async loadZidanData(ctx, params) {
      ctx.commit('SET_ZIDAN_LOADING', true)
      const _ret = await fetchZidanData(params)
      ctx.commit('SET_ZIDAN_LOADING', false)
      return _ret
    },
    changeCurrentSelectedGasCls(ctx, type) {
      ctx.commit('SET_CURRENT_SELECTED_GAS_CLS', type)
    },
    async loadRainData(ctx, params) {
      ctx.commit('SET_RAIN_LOADING', true)
      const { code, date } = await fetchRainData({
        meterGasClass: ctx.state.currentSelectedGasCls,
        ...params
      })
      ctx.commit('SET_RAIN_LOADING', false)
      if (code === 200) {
        ctx.commit('SET_RAIN_LIST', date)
      }
    },
    changeCurrentSelecteMapDate(ctx, date) {
      ctx.commit('SET_SELECTED_MAP_DATE', date)
    },
    async loadHeatMapData(ctx, params) {
      ctx.commit('SET_HEAT_MAP_LOADING', true)
      const { code, date } = await fetchHeatMapData({
        tId: ctx.state.currentSelectedMapDate || ctx.state.defaultDate,
        ...params
      })
      ctx.commit('SET_HEAT_MAP_LOADING', false)
      if (code === 200) {
        ctx.commit('SET_HEAT_MAP_LIST', date)
      }
    },
    changeMinimapCenter(ctx, center) {
      ctx.commit('SET_MINIMAP_CENTER', center)
    },
    changeCurrentMeterId(ctx, id) {
      ctx.commit('SET_CURRENT_METER_ID', id)
    },
    changeTablePageNum(ctx, num) {
      ctx.commit('SET_TABLE_PAGE_NUM', num)
    },
    async loadMeterTreeDetail(ctx, params) {
      const _ret = await fetchMeterUnitDetail(params)
      return _ret
    },
    refresh(ctx) {
      ctx.dispatch('clearStore')
      ctx.dispatch('loadTableData')
      ctx.dispatch('loadRainData')
      ctx.dispatch('loadZidanData')
      ctx.dispatch('loadHeatMapData')
    },
    changeHeatMapSwiperState(ctx, flag) {
      ctx.commit('SET_HEAT_MAP_SWIPER_STATE', flag)
    }
  }
}
